package org.dyk.mallmanagementsystem.controller;

import com.alibaba.fastjson.JSONObject;
import org.dyk.mallmanagementsystem.model.Orders;
import org.dyk.mallmanagementsystem.service.orders.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 订单管理页面
     * @return
     */
    @RequestMapping(value = {""})
    public String orders(@RequestParam(required = true, defaultValue = "0") int page,
                         @RequestParam(required = true, defaultValue = "10") int size,
                         Model model){
        Pageable pageable = PageRequest.of(page,size);
        Page<Orders> all = ordersService.findAll(pageable);
        model.addAttribute("pageAll",all);
        return "orders";
    }

    /**
     * 订单详情页面
     * @param oid
     * @param model
     * @return
     */
    @RequestMapping(value = {"/updataOrders"})
    public String updataOrders(@RequestParam int oid, Model model){
        //根据 主键 获取所有订单信息
        JSONObject byOid = ordersService.findByOid(oid);
        model.addAttribute("ordersData", byOid);
        return "updataOrders";
    }

    /**
     * 修改订单状态
     * @param oid
     * @return
     */
    @RequestMapping("/updataByState")
    @ResponseBody
    public boolean updataByState(@RequestParam int oid){
        //根据 主键 获取订单，修改订单状态
        boolean b = ordersService.updataByState(oid);
        if (b){
            return true;
        }else {
            return false;
        }
    }
}
